<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录页面</title>
    <style>
        body {
            background: url('https://cdn.pixabay.com/photo/2018/08/14/13/23/ocean-3605547_1280.jpg') no-repeat;
            background-size: 100% 130%;
        }

        #login_box {
            width: 282px;
            height: 400px;
            background-color: #00000060;
            margin: auto;
            margin-top: 10%;
            text-align: center;
            border-radius: 10px;
            padding: 50px 50px;
        }

        h2 {
            color: #ffffff90;
            margin-top: 5%;
        }

        #input-box {
            margin-top: 5%;
        }

        span {
            color: #fff;
        }

        input {
            border: 0;
            width: 60%;
            font-size: 15px;
            color: #fff;
            background: transparent;
            border-bottom: 2px solid #fff;
            padding: 5px 10px;
            outline: none;
            margin-top: 10px;
        }

        button {
            margin-top: 50px;
            width: 60%;
            height: 30px;
            border-radius: 10px;
            border: 0;
            color: #fff;
            text-align: center;
            line-height: 30px;
            font-size: 15px;
            background-image: linear-gradient(to right, #30cfd0, #330867);
        }

        #sign_up {
            margin-top: 45%;
            margin-left: 60%;
        }

        a {
            color: #b94648;
        }
    </style>
</head>

<body>
<div id="login_box">
    <h2>内蒙古京航特碳科技有限公司</h2>
    <div id="input_box">
        <input id="username" name="username" type="text" placeholder="请输入用户名">
    </div>
    <div class="input_box">
        <input id="password" name="password" type="password" placeholder="请输入密码">
    </div>
    <button id="submitBtn">登录</button>
    <br>
</div>

<script type="text/javascript"
        src="https://lf6-cdn-tos.bytecdntp.com/cdn/expire-1-M/jquery/1.7.2/jquery.min.js"></script>


<script type="text/javascript">
    $(function () {
        localStorage.removeItem("token")
        $("#submitBtn").click(function () {
            let username = $("#username").val();
            let password = $("#password").val();
            if (!username || !password) {
                alert("请输入用户名或密码！");
                return;
            }
            login(username,password)
        })
    });

    function login(username,password){
        $.ajax({
            contentType: 'application/json',
            url: '/login',
            type: 'POST',
            dataType: "json",
            data: JSON.stringify({
                "username": username,
                "password": password,
            }),
            success: function (result) {
                // 请求成功时的处理逻辑
                let resultData = result.data;
                localStorage.setItem('token', resultData.token);
                window.location = "/workTime.html"
            },
            error: function (xhr, status, error) {
                // 请求失败时的错误处理逻辑
                console.error('Error:', status, error);
                // 可以根据xhr对象的属性进行更详细的错误处理
                console.error('Error message:', xhr.responseText);
                var parse = JSON.parse(xhr.responseText);
                alert(parse.message)

            }
        });
    }

</script>

</body>
</html>
